import { Observable, catchError, of, throwError, Subscriber } from "rxjs";
//throwError是用来显示的抛出一个错误
const source = new Observable((subscriber) => {
  setTimeout(() => {
    // 抛出一个错误
    subscriber.error({ success: false });
  }, 1000);
});
source
  .pipe(
    // 捕获错误
    catchError((error) =>
      // 传递错误

      //  对比
      // .pipe(catchError((error) => of("正常值")))  会走 .subscribe({next: (val) => console.log("next", val)});
      // .pipe(throwError(() => ({ ...error, ts: Date.now() })))  会走 .subscribe({error: (error) => console.error("error", error)});

      throwError(() => ({ ...error, ts: Date.now() }))
    )
  )
  .subscribe({
    next: (val) => console.log("next", val),
    error: (error) => console.error("error", error),
  });

/*   catchErro 相当于 try catch
  throw Error 相当于  throw new Error */
